/*
 * IntegerField.java
 *
 * Created on 13 November 2006, 10:36
 */

package org.foment.joliage3.newt;

import com.toedter.calendar.JCalendar;
import java.awt.Component;
import java.awt.Dialog.ModalExclusionType;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author  ivan
 */
public class DateField extends javax.swing.JPanel {
  
  /** Creates new form BeanForm */
  public DateField() {
    initComponents();
    setCalendar();
    setBorder(txtValue.getBorder());
    txtValue.setBorder(null);
    
    txtValue.setText(String.valueOf(_value));
    
    txtValue.setFormatterFactory(new javax.swing.JFormattedTextField.AbstractFormatterFactory() {
      public javax.swing.JFormattedTextField.AbstractFormatter getFormatter(javax.swing.JFormattedTextField tf) {
        return new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance());
      }
    });
  }

  private void setCalendar()
  {
      cal = new JCalendar();
      for (final Component c : cal.getDayChooser().getComponents())
        {
                if(JButton.class.isInstance(c))
                    ((JButton)c).addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    String s = cal.getDayChooser().getDay()+ "."+(cal.getMonthChooser().getMonth()+1) + "."
                            +cal.getYearChooser().getYear()+".";
                    //System.out.println(s);
                    try {
                        setValue(java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT).parse(s));
                    } catch (ParseException ex) {
                        Logger.getLogger(DateField.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    f.dispose();

                }
            });
        }
      f = new JFrame();
      f.setSize(250, 200);
      f.add(cal);
      
      
  }
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtValue = new javax.swing.JFormattedTextField();

        setPreferredSize(new java.awt.Dimension(44, 19));
        setLayout(new java.awt.BorderLayout());

        txtValue.setEditable(false);
        txtValue.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtValue.setText("0");
        txtValue.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtValueMouseClicked(evt);
            }
        });
        txtValue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtValueActionPerformed(evt);
            }
        });
        txtValue.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtValueFocusLost(evt);
            }
        });
        add(txtValue, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
  
  private void txtValueFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtValueFocusLost
    try {
        if (!txtValue.getText().equals(""))
            setValue(java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT).parse(txtValue.getText()));
    } catch (ParseException ex) {
        if (_value != null)
            txtValue.setText(java.text.DateFormat.getDateInstance().format(_value));
    }
  }//GEN-LAST:event_txtValueFocusLost

  private void txtValueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtValueActionPerformed
      // TODO add your handling code here:
      f.setVisible(true);
  }//GEN-LAST:event_txtValueActionPerformed

  private void txtValueMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtValueMouseClicked
      // TODO add your handling code here:
      f.setVisible(true);
  }//GEN-LAST:event_txtValueMouseClicked
  
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField txtValue;
    // End of variables declaration//GEN-END:variables

  /**
   * Holds _value of property _value.
   */
  private java.util.Date _value = new java.util.Date();

  /**
   * Utility field used by bound properties.
   */
  private java.beans.PropertyChangeSupport propertyChangeSupport =  new java.beans.PropertyChangeSupport(this);

  /**
   * Utility field used by constrained properties.
   */
  private java.beans.VetoableChangeSupport vetoableChangeSupport =  new java.beans.VetoableChangeSupport(this);

  /**
   * Adds a PropertyChangeListener to the listener list.
   * @param l The listener to add.
   */
  public void addPropertyChangeListener(java.beans.PropertyChangeListener l) {
    propertyChangeSupport.addPropertyChangeListener(l);
  }

  /**
   * Removes a PropertyChangeListener from the listener list.
   * @param l The listener to remove.
   */
  public void removePropertyChangeListener(java.beans.PropertyChangeListener l) {
    propertyChangeSupport.removePropertyChangeListener(l);
  }

  /**
   * Getter for property value.
   * @return Value of property value.
   */
  public java.util.Date getValue() {
    //if (_value.equals(new java.util.Date(1))) return null;
    return _value;
  }

  public String getFormattedValue() {

      if( _value == null )
      {
          return "NULL";
      }
      String y = new java.text.SimpleDateFormat("yyyy").format(_value);
      String m = new java.text.SimpleDateFormat("MM").format(_value);
      String d = new java.text.SimpleDateFormat("dd").format(_value);

      return (y + "-" + m + "-" + d);
  } 
  /**
   * Setter for property value.
   * 
   * @param value New _value of property value.
   * @throws PropertyVetoException if some vetoable listeners reject the new _value
   */
  public void setValue(java.util.Date value) {
    //if (value == null) {
    //  value = new java.util.Date(1);
    //}
    java.util.Date oldValue = _value;
    _value = value;
    if (_value == null) 
      txtValue.setText("");
    else
      txtValue.setText(java.text.DateFormat.getDateInstance().format(_value));
    propertyChangeSupport.firePropertyChange ("value", oldValue, value);
  }
  JFrame f;
  JCalendar cal;
}
